jQuery(document).ready(function($) {
    var mouth = 10; //三年
    var d = new Date();
    var pid = $('#time');
    var sumType = 0;
    var chosetime = [];
    var body = $('body');
    var year = d.getFullYear();
    for (var i = 0; i < mouth; i++) {
        pid.append('<div class="year year-'+i+'">'+(year+i)+'</div>');
        var tempYearName = 'mouth-'+(year+i);
        var tempYearStr = '<div year="'+(year+i)+'" id="'+tempYearName+'" class="mouth mouth-'+i+'"></div>';
        pid.append(tempYearStr);
    }
    var mid = $('.mouth');
    for (var i = 0; i < mid.length; i++) {
        var temp = '';
        for (var j = 0; j < 12 ; j++) {
            var tempMouthName = mid.eq(i).attr('year')+'-'+(j+1);
            var timestamp = +new Date(tempMouthName);
            temp += '<div ontime="'+timestamp+'" class="fix '+tempMouthName+'"><span class="nextMouth">'+(j+1)+'月</span></div>';
        }
        mid.eq(i).append(temp);
    }
    body.on('click','.fix',function () {
        time = $(this).attr('ontime');
        choseTimeData(time,$(this));
        choseAddBackground($(this));
    });
    choseTimeData = function (time,that) {
        if(chosetime.length < 3){
            chosetime.push(time);
        }else{
            chosetime.splice(0,chosetime.length);//清空数组
            $('.c_chose').remove();//删除选中的全部
            chosetime.push(time);//开始时间
            console.log(chosetime);
        }
        addStartName(chosetime.length,that);
    };
    choseAddBackground = function (that) {
        $('div.fix').removeClass('e').removeClass('b');
        $bagin = $('.c_start').parent('div.fix').attr('ontime');
        $endTime = $('.c_end').parent('div.fix').attr('ontime');
        console.log($bagin);
        console.log($endTime);
        for (var i = 0; i < $('div.fix').length; i++) {
            tempTime = $('div.fix').eq(i).attr('ontime');
            if(tempTime <= $endTime){
                $('div.fix').eq(i).addClass('e');
            }
            if(tempTime >= $bagin){
                $('div.fix').eq(i).addClass('b');
            }
        }
    };
    addStartName = function (lenth,that) {
        if(that.find('.c_chose').length == 0){
            if(lenth == 1){
                that.append('<span class="c_start c_chose">开始时间</span>')
            }else if (lenth == 2) {
                that.append('<span class="c_end c_chose">结束时间</span>');
            }else{
                $('.c_start').remove();// 删除第一个开始时间
                that.append('<span class="c_start c_chose">开始时间</span>');
            }
            $baginTime = $('.c_start').parent('div.fix').attr('ontime');
            $endTime = $('.c_end').parent('div.fix').attr('ontime');
            if($endTime < $baginTime){
                $('.c_start').html('结束时间');
                $('.c_end').html('开始时间');
                changeDomchoseName(that);//改class名称
            }
        }else{
            console.log('当前节点已被选中');
        }
        console.log(chosetime);
    };
    changeDomchoseName = function (that) {
        body.find('.c_end').removeClass('c_end').addClass('c_start_bak');
        body.find('.c_start').removeClass('c_start').addClass('c_end_bak');

        body.find('.c_end_bak').removeClass('c_end').addClass('c_end');
        body.find('.c_start_bak').removeClass('c_start_bak').addClass('c_start');
    };
});